<?php
if (!defined('admin')) {
    exit();
}
admin_page_start("多语言", array(array('url' => '?do=lang', 'title' => '返回语言文件管理')));
if (!MLang) {
    adminmsg("", "多语言功能未开启");
}

?>
    <h3>多语言管理-翻译</h3>
    <style>
        .tableEditor input[type=text] {
            width: 100%;
        }

        #newLine {
            display: none;
        }
    </style>
<?php $langs = langGetLangFiles();
$langs_data = array();
?>
    <table id="newLine">
        <tbody>
        <tr>
            <td>
                <button onclick="delline(this)" class="btn btn-danger btn-xs">-</button>
            </td>
            <td><input class="form-control" type="text"></td>
            <?php foreach ($langs as $k => $v): ?>
                <td><input class="form-control" type="text">
                </td>
            <?php endforeach; ?>
        </tr>
        </tbody>
    </table>
    <table class="tableBasic tableEditor table-bordered" width="100%" border="0" cellpadding="8" cellspacing="0">
        <tr>
            <th>删</th>
            <th>键</th><?php
            foreach ($langs as $k => $v):
                $langs_data[$v['name']] = langGetFile($v['file']);
                ?>
                <th data-lang="<?php echo $v['file'] ?>"><?php echo $v['name'] . ' [' . $v['file'] . ']' ?></th>
            <?php endforeach; ?>
        </tr>

        <?php
        $lang_keys = langGetKeys();
        foreach ($lang_keys as $key): ?>
            <tr>
                <td>
                    <button onclick="delline(this)" class="btn btn-danger btn-xs">-</button>
                </td>
                <td><input class="form-control" type="text" value="<?php echo $key ?>"></td>
                <?php foreach ($langs as $k => $v): ?>
                    <td><input class="form-control" type="text" value="<?php
                        if (isset($langs_data[$v['name']][$key])) {
                            echo $langs_data[$v['name']][$key];
                        } else {
                            echo '';
                        }
                        ?>"></td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </table>

    <p style="text-align: center">
        <br>
        <button class="btn btn-success" onclick="newline()">新增一行</button>
    </p>
    <hr>
    <p style="text-align: center">
        <button class="btn btn-primary" onclick="saveLangs()">保存</button>
    </p>
    <script>
        function newline() {
            $(".tableEditor tbody").append($("#newLine tbody").html())
        }

        function delline(_this) {
            $(_this).closest("tr").remove();
        }

        function saveLangs() {
            var trs = $(".tableBasic tr")
            var langs = {};
            var ths = $(trs[0]).find("th")
            var keys=[];

            for (var j = 1; j < trs.length; j++) {
                var key = $($(trs[j]).find("td")[1]).find("input").val()
                keys.push(key)
            }

            for (var i = 2; i < ths.length; i++) {
                var langfile = $(ths[i]).data("lang").replace(".json", '');
                langs[langfile] = {};
                for (var j = 1; j < trs.length; j++) {
                    var key = keys[j-1];
                    langs[langfile][key] = $($(trs[j]).find("td")[i]).find("input").val()
                }
            }
            console.log(langs)
            // langs = JSON.stringify(langs);
            // console.log(langs)
            $.post('?do=lang_editorsave&ajax=1', {data: langs,keys:keys}, function (d) {
                // console.log(d)
                try {
                    d = JSON.parse(d);
                    if (d && d['r'] === 1) {
                        alert("保存成功")
                    } else {
                        alert(d['msg']);
                    }
                } catch (e) {
                    console.log(e)
                    alert("服务错误");
                }
            })
        }
    </script>
<?php
admin_page_end();